//
//  CountryManager.m
//  CountryInfoFromVisualDataFlex
//
//  Created by 안종덕 on 11. 3. 26.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "CountryManager.h"
//#import "CountryInfoService.h"

@implementation CountryManager
-(id)init {
    binding = [[CountryInfoService CountryInfoServiceSoapBinding] retain];
    binding.logXMLInOut = NO;
    [super init];
    return self;
}

-(void)dealloc {
    [super dealloc];
//    [binding release];
}

-(NSArray *)getContinents {
    NSMutableArray *continents = nil;
    CountryInfoService_ListOfContinentsByCode *request = [[CountryInfoService_ListOfContinentsByCode new] autorelease];
    CountryInfoServiceSoapBindingResponse *response = [binding ListOfContinentsByCodeUsingParameters:request];
    if(response == nil) {
        ALog(@"서버로부터 대륙정보를 입수하지 못했습니다.\n");
    } else {
        NSArray *headers = response.headers;
        NSArray *bodyParts =response.bodyParts;
        for(id header in headers) {
            // 헤더 응답정보들에 대한 특별한 처리는 하지 않는다.
        }
        int ccount = 0;
        for(id bodyPart in bodyParts) {
            if([bodyPart isKindOfClass:[SOAPFault class]]) {
                ALog(@"가져올 bodyPart가 없넹\n");
                continue;
            }
            if([bodyPart isKindOfClass:[CountryInfoService_ListOfContinentsByCodeResponse class]]) {
                CountryInfoService_ListOfContinentsByCodeResponse *body = (CountryInfoService_ListOfContinentsByCodeResponse *)bodyPart;
                continents = body.ListOfContinentsByCodeResult.tContinent;
                ALog(@"%d\n", [continents count]);
                ccount++;
            }
        }
        ALog(@"가져온 bodyPart 수: %d\n", ccount);
    }
//    [request release];
    return continents;
}

-(NSArray *)getLanguages {
    NSMutableArray *languages = nil;
    CountryInfoService_ListOfLanguagesByCode *request = [[CountryInfoService_ListOfLanguagesByCode new] autorelease];
    CountryInfoServiceSoapBindingResponse *response = [binding ListOfLanguagesByCodeUsingParameters:request];
    if(response == nil) {
        ALog(@"서버로부터 언어정보를 입수하지 못했습니다.\n");
    } else {
        NSArray *headers = response.headers;
        NSArray *bodyParts =response.bodyParts;
        for(id header in headers) {
            // 헤더 응답정보들에 대한 특별한 처리는 하지 않는다.
        }
        int ccount = 0;
        for(id bodyPart in bodyParts) {
            if([bodyPart isKindOfClass:[SOAPFault class]]) {
                ALog(@"가져올 bodyPart가 없넹\n");
                continue;
            }
            if([bodyPart isKindOfClass:[CountryInfoService_ListOfLanguagesByCodeResponse class]]) {
                CountryInfoService_ListOfLanguagesByCodeResponse *body = (CountryInfoService_ListOfLanguagesByCodeResponse *)bodyPart;
                languages = body.ListOfLanguagesByCodeResult.tLanguage;
                ALog(@"%d\n", [languages count]);
                ccount++;
            }
        }
        ALog(@"가져온 bodyPart 수: %d\n", ccount);
    }
//    [request release];
    return languages;
}

-(NSArray *)getCurrencies {
    NSMutableArray *currencies = nil;
    CountryInfoService_ListOfCurrenciesByCode *request = [[CountryInfoService_ListOfCurrenciesByCode new] autorelease];
    CountryInfoServiceSoapBindingResponse *response = [binding ListOfCurrenciesByCodeUsingParameters:request];
    if(response == nil) {
        ALog(@"서버로부터 통화정보를 입수하지 못했습니다.\n");
    } else {
        NSArray *headers = response.headers;
        NSArray *bodyParts =response.bodyParts;
        for(id header in headers) {
            // 헤더 응답정보들에 대한 특별한 처리는 하지 않는다.
        }
        int ccount = 0;
        for(id bodyPart in bodyParts) {
            if([bodyPart isKindOfClass:[SOAPFault class]]) {
                ALog(@"가져올 bodyPart가 없넹\n");
                continue;
            }
            if([bodyPart isKindOfClass:[CountryInfoService_ListOfCurrenciesByCodeResponse class]]) {
                CountryInfoService_ListOfCurrenciesByCodeResponse *body = (CountryInfoService_ListOfCurrenciesByCodeResponse *)bodyPart;
                currencies = body.ListOfCurrenciesByCodeResult.tCurrency;
                ALog(@"%d\n", [currencies count]);
                ccount++;
            }
        }
        ALog(@"가져온 bodyPart 수: %d\n", ccount);
    }
    //    [request release];
    return currencies;
}

-(NSArray *)getCountries {
    NSMutableArray *countries = nil;
    CountryInfoService_FullCountryInfoAllCountries *request = [[CountryInfoService_FullCountryInfoAllCountries new] autorelease];
    CountryInfoServiceSoapBindingResponse *response = [binding FullCountryInfoAllCountriesUsingParameters:request];
    if(response == nil) {
        ALog(@"서버로부터 통화정보를 입수하지 못했습니다.\n");
    } else {
        NSArray *headers = response.headers;
        NSArray *bodyParts =response.bodyParts;
        for(id header in headers) {
            // 헤더 응답정보들에 대한 특별한 처리는 하지 않는다.
        }
        int ccount = 0;
        for(id bodyPart in bodyParts) {
            if([bodyPart isKindOfClass:[SOAPFault class]]) {
                ALog(@"가져올 bodyPart가 없넹\n");
                continue;
            }
            if([bodyPart isKindOfClass:[CountryInfoService_FullCountryInfoAllCountriesResponse class]]) {
                CountryInfoService_FullCountryInfoAllCountriesResponse *body = (CountryInfoService_FullCountryInfoAllCountriesResponse *)bodyPart;
                countries = body.FullCountryInfoAllCountriesResult.tCountryInfo;
                ALog(@"%d\n", [countries count]);
                ccount++;
            }
        }
        ALog(@"가져온 bodyPart 수: %d\n", ccount);
    }
    //    [request release];
    return countries;
}

@end
